package com.songtech.elefence.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.songtech.base.universal.dao.BaseDao;
import com.songtech.server.constant.Calculate;
import com.songtech.web.entry.SysPosElefence;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

/**
 * Create by WH on 2018/4/11 11:15
 *
 * @Description :
 */

@Service
public class EleFenceService {
    @Resource(name = "com.songtech.web.entry.SysPosElefence")
    private BaseDao<SysPosElefence> baseDaoImpl;

    /**
     * 新增--电子围栏
     * @param fence
     * @return
     * @throws SQLException
     */
    public int insertFence(SysPosElefence fence) throws SQLException {
        return baseDaoImpl.insert(fence);
    }


    /**
     * 查询电子围栏列表，不含分页
     * @param fence
     * @return
     * @throws SQLException
     */
    public PageInfo<SysPosElefence> selectFenceList(SysPosElefence fence) throws SQLException {

        List<SysPosElefence> list = baseDaoImpl.select(fence);

        return new PageInfo<SysPosElefence>(list);
    }
    /**
     * 根据实体查询--电子围栏，分页查询
     * @param fence
     * @return
     * @throws SQLException
     */
    public PageInfo<SysPosElefence> selectFenceList(SysPosElefence fence, int pageSize, int pageNum) throws SQLException {

        // 分页,禁用合理化，如果pageNum<1或pageNum>pages会返回空数据
        PageHelper.startPage(pageNum, pageSize,true,false);

        //排序
        PageHelper.orderBy(" create_time desc,update_time desc");

        List<SysPosElefence> list = baseDaoImpl.select(fence);

        if(list != null && list.size() > 0){
            for (int i = 0 ; i < list.size(); i++){
                //经纬度转换
                list.get(i).setLongitudeShow(Double.valueOf(list.get(i).getCenterLongitude()) / Calculate.LON_LAT_FACTOR); //显示给前台的经纬度
                list.get(i).setLatitudeShow(Double.valueOf(list.get(i).getCenterLatitude()) / Calculate.LON_LAT_FACTOR);
            }
        }
        return new PageInfo<SysPosElefence>(list);
    }

    /**
     * 删除--电子围栏
     * @param id 主键
     * @return
     * @throws SQLException
     */
    public int deleteFence(Integer id) throws SQLException {
        return baseDaoImpl.delete(id);
    }

    /**
     * 更新--电子围栏 (只更新 不为null 的字段)
     * <br>根据主键进行更新
     * @param fence
     * @return
     * @throws SQLException
     */
    public int updateFence(SysPosElefence fence) throws SQLException {
        return baseDaoImpl.updateNotEmpty(fence);
    }


}
